package summary;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: 海琳琦
 * @Date: 2022/6/17 10:23
 * https://leetcode.cn/problems/fruit-into-baskets/
 */
public class Title904 {

    public int totalFruit(int[] fruits) {
        Set<Integer> hash = new HashSet<>();
        int max = 0, left = 0;
        for (int right = 0; right <fruits.length ; right++) {
            hash.add(fruits[right]);
            if (hash.size() > 2) {
                for (int j = right - 1; j >= 0; j--) {
                    if (fruits[j] != fruits[j - 1]) {
                        left = j;
                        break;
                    }
                }
                hash.remove(fruits[left - 1]);
            }else{
                max = Math.max(max, right - left + 1);
            }
        }
        return max;
    }

    public static void main(String[] args) {

    }
}
